﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UIManager : MonoBehaviour {
    private GameObject m_Start_UI;
    private GameObject m_Game_UI;
    private GameObject m_Shop_UI;
    private GameObject m_playbtn;
    private GameObject m_shopbtn;
    private GameObject m_left;
    private GameObject m_right;
    private GameObject m_pausebtn;
    private GameObject m_continuebtn;
    private GameObject m_exitshopbtn;
    private GameObject m_buybtn;

    private UILabel m_Start_TopScore_Label;
    private UILabel m_Start_GemCount_Label;
    private UILabel m_Game_Score_Label;
    private UILabel m_Game_GemCount_Label;
    private UILabel m_Shop_GemCount_Label;

    private PlayerController m_playercontroller;
    private MapManager m_mapManager;
    private ShopManager m_shopManager;

    void Start()
    {
        m_Start_UI = GameObject.Find("Start_UI");
        m_Game_UI = GameObject.Find("Game_UI");
        m_Shop_UI = GameObject.Find("Shop_UI");
        m_left = GameObject.Find("Left");
        m_right = GameObject.Find("Right");

        m_Start_GemCount_Label = GameObject.Find("Start_GemCount_Label").GetComponent<UILabel>();
        m_Start_TopScore_Label = GameObject.Find("Start_TopScore_Label").GetComponent<UILabel>();
        m_Game_GemCount_Label = GameObject.Find("Game_GemCount_Label").GetComponent<UILabel>();
        m_Game_Score_Label = GameObject.Find("Game_Score_Label").GetComponent<UILabel>();
        m_Shop_GemCount_Label = GameObject.Find("Shop_GemCount_Label").GetComponent<UILabel>();

        m_playercontroller = GameObject.Find("Player").GetComponent<PlayerController>();
        m_mapManager = GameObject.Find("MapManager").GetComponent<MapManager>();
        m_shopManager = GameObject.Find("MapManager").GetComponent<ShopManager>();

        m_playbtn = GameObject.Find("Start_Play_Button");
        m_shopbtn = GameObject.Find("Start_Shop_Button");
        m_continuebtn = GameObject.Find("Game_Play_Button");
        m_pausebtn = GameObject.Find("Game_Pause_Button");
        m_exitshopbtn = GameObject.Find("Shop_Play_Button");
        m_buybtn = GameObject.Find("Shop_Buy_Button");

        UIEventListener.Get(m_playbtn).onClick = PlayOnButtonClick;
        UIEventListener.Get(m_shopbtn).onClick = ShopOnButtonClick;
        UIEventListener.Get(m_pausebtn).onClick = PauseOnButtonClick;
        UIEventListener.Get(m_continuebtn).onClick = ContinueOnButtonClick;
        UIEventListener.Get(m_exitshopbtn).onClick = ExitShopOnButtonClick;
        UIEventListener.Get(m_buybtn).onClick = BuyOnButtonClick;

        UIEventListener.Get(m_left).onClick = Left;
        UIEventListener.Get(m_right).onClick = Right;

        Init();
        m_Game_UI.SetActive(false);
        m_Shop_UI.SetActive(false);
    }

    void Update()
    {
        UpdateData(m_playercontroller.scorecount, m_playercontroller.gemCount);
    }

    void Init()
    {
        m_Start_TopScore_Label.text = "最佳\n"+PlayerPrefs.GetInt("TopScore", 0) + "";
        m_Start_GemCount_Label.text = PlayerPrefs.GetInt("GemCount", 0) + "";
        m_Game_GemCount_Label.text = PlayerPrefs.GetInt("GemCount", 0) + "";
        m_Shop_GemCount_Label.text = PlayerPrefs.GetInt("GemCount", 0) + "";
        m_Game_Score_Label.text = "0";
    }
	
    private void PlayOnButtonClick(GameObject go)
    {
        Debug.Log("游戏开始啦");
        m_Start_UI.SetActive(false);
        m_Game_UI.SetActive(true);
        m_continuebtn.SetActive(false);
        m_playercontroller.StartGame();
    }	

    private void ShopOnButtonClick(GameObject go)
    {
        m_shopManager.EnterShop();
        m_Start_UI.SetActive(false);
        m_Shop_UI.SetActive(true);
    }

    private void ExitShopOnButtonClick(GameObject go)
    {
        m_shopManager.ExitShop();
        m_mapManager.ResetGameMap();
        m_Start_UI.SetActive(true);
        m_Shop_UI.SetActive(false);
    }

    private void BuyOnButtonClick(GameObject go)
    {
        if (m_playercontroller.gemCount >= 100)
        {
            m_playercontroller.Pay();
            m_shopManager.Buy();
        }
    }

    private void ContinueOnButtonClick(GameObject go)
    {
        m_pausebtn.SetActive(true);
        m_mapManager.StartFallDown();
        m_playercontroller.life = true;
        m_continuebtn.SetActive(false);
    }

    private void PauseOnButtonClick(GameObject go)
    {
        m_continuebtn.SetActive(true);
        m_mapManager.StopFallDown();
        m_playercontroller.life = false;
        m_pausebtn.SetActive(false);
    }

    public void UpdateData(int scorecount, int gemCount)
    {
        m_Game_Score_Label.text = scorecount + "";
        m_Game_GemCount_Label.text = gemCount + "";
        m_Start_GemCount_Label.text = gemCount + "";
        m_Shop_GemCount_Label.text = gemCount + "";
        m_Start_TopScore_Label.text = "最佳\n" + PlayerPrefs.GetInt("TopScore", 0) + "";
    }

    public void ResetUI()
    {
        m_Start_UI.SetActive(true);
        m_Game_UI.SetActive(false);
        
    }

    private void Left(GameObject go)
    {
        if (m_playercontroller.life == true)
        {
            m_playercontroller.Left();
        }
    }

    private void Right(GameObject go)
    {
        if (m_playercontroller.life == true)
        {
            m_playercontroller.Right();
        }
    }
}
